package com.hpin.fansea.home;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.hpin.fansea.R;
import com.hpin.fansea.base.BaseActivity;
import com.hpin.fansea.bean.ComplaintDetail;
import com.hpin.fansea.bean.ResultBaseBean;
import com.hpin.fansea.utils.Constant;
import com.hpin.fansea.utils.HttpUtil;
import com.lidroid.xutils.http.RequestParams;

import java.util.Date;

/**
 * 投诉
 * Created by Aaron on 15/9/14.
 */
public class ComplaintActivity extends BaseActivity {

    private EditText et_complaint;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_complaint);

        initWidget();

    }

    private void initWidget() {

        ImageView iv_left = (ImageView) findViewById(R.id.iv_left);
        TextView tv_title = (TextView) findViewById(R.id.tv_title);

        tv_title.setText(getResources().getString(R.string.complaint));

        iv_left.setOnClickListener(this);

        et_complaint = (EditText) findViewById(R.id.et_complaint);

        Button btn_commit = (Button) findViewById(R.id.btn_commit);

        ImageView iv_call = (ImageView) findViewById(R.id.iv_call);

        btn_commit.setOnClickListener(this);
        iv_call.setOnClickListener(this);

    }

    @Override
    public void widgetClick(View v) {

        switch (v.getId()) {

            case R.id.iv_left:

                finish();
                break;

            case R.id.btn_commit: //提交投诉

                if (TextUtils.isEmpty(et_complaint.getText().toString().trim())) {

                    showToast("请输入您的投诉内容");
                    return;
                }

                uploadComplaint();

                break;

            case R.id.iv_call:

                //拨打客服电话
                new AlertDialog.Builder(ComplaintActivity.this).setTitle("提示").setMessage("010-62389999").setPositiveButton("确定",
                        new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface arg0, int arg1) {

//                                String phoneNum = surround_detail_phone.getText().toString().trim();

//                                if (!TextUtils.isEmpty(phoneNum)) {
                                    Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + "01062389999"));
                                    startActivity(intent);
//                                }

                            }
                        }).setNegativeButton("取消", null).show();

                break;

        }
    }

    //提交投诉
    private void uploadComplaint() {

        ComplaintDetail complaintDetail = new ComplaintDetail();

        complaintDetail.tal = sp.getString(Constant.mobile, "");
        complaintDetail.introduction = "";
        complaintDetail.startPerson = sp.getString(Constant.userName, "");
        complaintDetail.comments = et_complaint.getText().toString().trim();
        complaintDetail.createTime = new Date().toLocaleString();
        complaintDetail.feedbackTypeId = Constant.SUGGEST_BACK;

        String jsonString = JSONObject.toJSONString(complaintDetail);

        RequestParams params = new RequestParams();
        params.addQueryStringParameter("token", sp.getString(Constant.token, ""));
        params.addQueryStringParameter("systemType", Constant.SYSTEM_TYPE);
        params.addQueryStringParameter("feedback", jsonString);

        HttpUtil.getInstance().setPostRequest("提交中...", this, Constant.WEB_ROOT + Constant.SUGGESTBACK, params, new HttpUtil.SuccessListener() {
            @Override
            public void setOnSuccessListener(String str) {

                try {

                    ResultBaseBean resultBaseBean = JSONObject.parseObject(str, ResultBaseBean.class);

                    if (Constant.STATUS_SUCESS.equals(resultBaseBean.status)) {

                        showToast("投诉成功");
                        finish();

                    }else {
                        showToast(resultBaseBean.message);
                    }

                }catch (Exception e) {
                    e.printStackTrace();
                    showToast("投诉失败");
                }

            }
        }, null);

    }
}
